using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using PlugNT.Safe;
using PlugNT.Database;
using PlugNT.Database.Common.Simple;
using PlugNT.Cms.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.DAL
{
    public class FriendLink
    {

        private static string currTableName = WebConfig.TablePrefix + "friendlink";
        private static Dictionary<string, string> links;
        private const string linkTemplet = "<a href='{0}' target='_blank' title='{1}'>{2}</a>";

        static FriendLink()
        {
            links = new Dictionary<string, string>();
        }


        private int GetClassId(int id)
        {
            string sql = "Select top 1 [class_id] From " + currTableName + " where [id]=" + id.ToString();
            object obj = DbHelper.GetSingle(sql);
            return (obj == null) ? 0 : int.Parse(obj.ToString());
        }







        private string GetLinks(int classId)
        {
            StringBuilder sb = new StringBuilder();
            IDataReader dr = GetReader(-1, "*", " and class_id=" + classId);
            bool firFlag = true;
            if (dr != null)
            {
                while(dr.Read())
                {
                    if (!firFlag)
                    {
                        sb.Append("{0}");
                    }
                    sb.Append(string.Format(linkTemplet, dr["target_url"].ToString(), dr["description"].ToString(), dr["text"].ToString()));
                    firFlag = false;
                }
                dr.Close();
            }
            return sb.ToString();
        }







        public string CreateLinks(int classId, string splitString)
        {
            string reString = "";
            if (!links.TryGetValue(classId.ToString(), out reString))
            {
                reString = GetLinks(classId);
                links.Add(classId.ToString(), reString);
            }
            return string.Format(reString, splitString);
        }






        public void RemoveLinks()
        {
            links.Clear();
        }






        public static bool RemoveLinks(int classId)
        {
            bool flag = false;
            if (links.ContainsKey(classId.ToString()))
            {
                flag = links.Remove(classId.ToString());
            }
            return flag;
        }







        public FriendLinkInfo GetFriendLinkInfo(int id)
        {
            FriendLinkInfo model = null;
            IDataReader dr = GetReader(1, "*", " and id=" + id);
            if (dr != null)
            {
                if (dr.Read())
                {
                    model = new FriendLinkInfo();
                    model.sys_model_id = int.Parse(dr["sys_model_id"].ToString());
                    model.id = int.Parse(dr["id"].ToString());
                    model.class_id = int.Parse(dr["class_id"].ToString());
                    model.target_url = dr["target_url"].ToString();
                    model.description = dr["description"].ToString();
                    model.text = dr["text"].ToString();
                    model.order = int.Parse(dr["order"].ToString());
                }
                dr.Close();
            }
            return model;
        }











        public bool Add(FriendLinkInfo model)
        {
            RemoveLinks(model.class_id); 

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName);
            strSql.Append(" (sys_model_id,[class_id],[target_url],[description],[text],[order]) ");
            strSql.Append(" values (");
            strSql.Append(" " + model.sys_model_id + ",");
            strSql.Append(" " + model.class_id + ",");
            strSql.Append("'" + StringHelper.SqlFilter(model.target_url) + "',");
            strSql.Append("'" + StringHelper.SqlFilter(model.description) + "',");
            strSql.Append("'" + StringHelper.SqlFilter(model.text) + "',");
            strSql.Append(" " + model.order.ToString());
            strSql.Append(")");
            
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }






        public bool Update(FriendLinkInfo model)
        {
            int classId1 = model.class_id;
            int classId2 = GetClassId(model.id);

            if (classId1 != classId2)
            {
                RemoveLinks(classId1);
                RemoveLinks(classId2);
            }
            else
            {
                RemoveLinks(classId1);
            }
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + currTableName);
            strSql.Append(" set [class_id]=" + model.class_id + ",");
            strSql.Append("[target_url]='" + StringHelper.SqlFilter(model.target_url) + "',");
            strSql.Append("[description]='" + StringHelper.SqlFilter(model.description) + "',");
            strSql.Append("[text]='" + StringHelper.SqlFilter(model.text) + "',");
            strSql.Append("[order]=" + model.order.ToString());
            strSql.Append(" where id=" + model.id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }






        public bool Delete(int id)
        {

            RemoveLinks(GetClassId(id));

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete From " + currTableName);
            strSql.Append(" where [id]="+id.ToString());
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }






        public bool DeleteByClassId(int classId)
        {

            RemoveLinks(classId);

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete From " + currTableName);
            strSql.Append(" where class_id= " + classId);
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }










        private IDataReader GetReader(int topCount, string colNames, string andWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select " + (topCount == -1 ? "" : "top " + topCount) + " " + colNames + " from " + currTableName + " where 1=1 " + andWhere);
            strSql.Append(WebConfig.IsAccess ? " order by [order] desc" : " order by [order],id desc");
            return DbHelper.ExecuteReader(strSql.ToString());
        }

        public DataTable GetList(int sysModelId, string andWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from " + currTableName + " where sys_model_id=" + sysModelId +  andWhere);
            strSql.Append(WebConfig.IsAccess ? " order by [order] desc" : " order by [order],id desc");
            return DbHelper.GetDataTable(strSql.ToString());
        }

        public DataTable GetList(int sysModelId, int topCount, int currPageCount, string andWhere, LeftJoinInfo joininfo)
        {
            string sql = Sql.GetRePagingString(topCount, currPageCount, currTableName, "id", "*", " and sys_model_id=" + sysModelId + andWhere, joininfo);
            return DbHelper.GetDataTable(sql);
        }

        public int GetCount(int sysModelId, string andWhere)
        {
            string sql = "select count(1) from [" + currTableName + "] where sys_model_id=" + sysModelId + andWhere;
            return (int)DbHelper.GetSingle(sql);
        }

    }
}

